﻿using System.Windows;
using System.Windows.Controls;
using GameEditor.View.Components;
using GameEngine.Model.Data;
using GameEngine.View.Components;
using System.Collections.Generic;

namespace SilverlightClientEditor.View.Components
{
    public partial class GamePropertiesEditorUC
        : UserControl, IGameEditorMenuView<int>
    {
        public event OnButtonClickHandler<IGameEditorMenuViewConstants.ButtonTags> OnExternalActionButtonClick;

        public GamePropertiesEditorUC()
        {
            InitializeComponent();
            EditResourceButton.Tag = IGameEditorMenuViewConstants.ButtonTags.EDITRESOURCE;
            NewResourceButton.Tag = IGameEditorMenuViewConstants.ButtonTags.NEWRESOURCE;
            RemoveResourceButton.Tag = IGameEditorMenuViewConstants.ButtonTags.REMOVERESOURCE;
            CopyAsNewResourceButton.Tag = IGameEditorMenuViewConstants.ButtonTags.COPYASNEWRESOURCE;

            EditLevelButton.Tag = IGameEditorMenuViewConstants.ButtonTags.EDITLEVEL;
            NewLevelButton.Tag = IGameEditorMenuViewConstants.ButtonTags.NEWLEVEL;
            RemoveLevelButton.Tag = IGameEditorMenuViewConstants.ButtonTags.REMOVELEVEL;
            //CopyAsNewLevelButton.Tag = IGameEditorMenuViewConstants.ButtonTags.COPYASNEWLEVEL;

            EditViewButton.Tag = IGameEditorMenuViewConstants.ButtonTags.EDITVIEW;
            NewViewButton.Tag = IGameEditorMenuViewConstants.ButtonTags.NEWVIEW;
            RemoveViewButton.Tag = IGameEditorMenuViewConstants.ButtonTags.REMOVEVIEW;
            CopyAsNewViewButton.Tag = IGameEditorMenuViewConstants.ButtonTags.COPYASNEWVIEW;

            EditViewStateButton.Tag = IGameEditorMenuViewConstants.ButtonTags.EDITVIEWSTATE;
            NewViewStateButton.Tag = IGameEditorMenuViewConstants.ButtonTags.NEWVIEWSTATE;
            RemoveViewStateButton.Tag = IGameEditorMenuViewConstants.ButtonTags.REMOVEVIEWSTATE;

            EditThingTypeButton.Tag = IGameEditorMenuViewConstants.ButtonTags.EDITTHINGTYPE;
            NewThingTypeButton.Tag = IGameEditorMenuViewConstants.ButtonTags.NEWTHINGTYPE;
            RemoveThingTypeButton.Tag = IGameEditorMenuViewConstants.ButtonTags.REMOVETHINGTYPE;
            CopyAsNewThingTypeButton.Tag = IGameEditorMenuViewConstants.ButtonTags.COPYASNEWTHINGTYPE;

            SetButton.Tag = IGameEditorMenuViewConstants.ButtonTags.SET;
            CancelButton.Tag = IGameEditorMenuViewConstants.ButtonTags.CANCEL;

            EditLevelComboBox.DisplayMemberPath = "Name";
            EditViewComboBox.DisplayMemberPath = "Name";
            EditThingTypeComboBox.DisplayMemberPath = "Name";
            EditViewStateComboBox.DisplayMemberPath = "Value";
        }

        public string GameName {
            get { return NameTextBox.Text; }
            set { NameTextBox.Text = value ?? ""; }
        }

        public Point TileSize
        {
            get
            {
                return new Point(double.Parse(TileSizeXTextBox.Text), double.Parse(TileSizeYTextBox.Text));
            }
            set
            {
                TileSizeXTextBox.Text = value.X.ToString();
                TileSizeYTextBox.Text = value.Y.ToString();
            }
        }

        public float MaxMapHeight
        {
            get
            {
                return float.Parse(MaxMapHeightTextBox.Text);
            }
            set
            {
                MaxMapHeightTextBox.Text = value.ToString();
            }
        }

        public float MinMapHeight
        {
            get
            {
                return float.Parse(MinMapHeightTextBox.Text);
            }
            set
            {
                MinMapHeightTextBox.Text = value.ToString();
            }
        }

        public float TileHeightUnit
        {
            get
            {
                return float.Parse(TileHeightUnitTextBox.Text);
            }
            set
            {
                TileHeightUnitTextBox.Text = value.ToString();
            }
        }

        public void AddResource(IResourceVOId id) { EditResourceComboBox.Items.Add(id); }
        public void RemoveResource(IResourceVOId id) { EditResourceComboBox.Items.Remove(id); }
        public List<IResourceVOId> GetResources()
        {
            List<IResourceVOId> result = new List<IResourceVOId>();
            foreach (var obj in EditResourceComboBox.Items)
            {
                result.Add(obj as IResourceVOId);
            }
            return result;
        }
        public IResourceVOId SelectedResource
        {
            get { return EditResourceComboBox.SelectedItem as IResourceVOId; }
        }

        public void AddLevel(ILevelVOId id) { 
            EditLevelComboBox.Items.Add(id);
            if (EditLevelComboBox.SelectedItem == null)
                EditLevelComboBox.SelectedItem = id;
        }
        public void RemoveLevel(ILevelVOId id) { EditLevelComboBox.Items.Remove(id); }
        public List<ILevelVOId> GetLevels()
        {
            List<ILevelVOId> result = new List<ILevelVOId>();
            foreach (var obj in EditLevelComboBox.Items) {
                result.Add(obj as ILevelVOId);
            }
            return result;
        }
        public ILevelVOId SelectedLevel
        {
            get { return EditLevelComboBox.SelectedItem as ILevelVOId; }
        }

        public void AddView(IViewVOId<int> id) { EditViewComboBox.Items.Add(id); }
        public void RemoveView(IViewVOId<int> id) { EditViewComboBox.Items.Remove(id); }
        public List<IViewVOId<int>> GetViews()
        {
            List<IViewVOId<int>> result = new List<IViewVOId<int>>();
            foreach (var obj in EditViewComboBox.Items)
            {
                result.Add(obj as IViewVOId<int>);
            }
            return result;
        }
        public IViewVOId<int> SelectedView
        {
            get { return EditViewComboBox.SelectedItem as IViewVOId<int>; }
        }

        public void AddViewState(IViewStateVO id) { EditViewStateComboBox.Items.Add(id); }
        public void RemoveViewState(IViewStateVO id) { EditViewStateComboBox.Items.Remove(id); }
        public List<IViewStateVO> GetViewStates()
        {
            List<IViewStateVO> result = new List<IViewStateVO>();
            foreach (var obj in EditViewComboBox.Items)
            {
                result.Add((IViewStateVO)obj);
            }
            return result;
        }
        public IViewStateVO SelectedViewState
        {
            get { return (ViewStateVO)EditViewStateComboBox.SelectedItem ; }
        }

        public void AddThingType(IThingTypeVOId id) { EditThingTypeComboBox.Items.Add(id); }
        public void RemoveThingType(IThingTypeVOId id) { EditThingTypeComboBox.Items.Remove(id); }
        public List<IThingTypeVOId> GetThingTypes()
        {
            List<IThingTypeVOId> result = new List<IThingTypeVOId>();
            foreach (var obj in EditThingTypeComboBox.Items)
            {
                result.Add(obj as IThingTypeVOId);
            }
            return result;
        }
        public IThingTypeVOId SelectedThingType
        {
            get { return EditThingTypeComboBox.SelectedItem as IThingTypeVOId; }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(OnExternalActionButtonClick!=null)
                OnExternalActionButtonClick((IGameEditorMenuViewConstants.ButtonTags)((Button)sender).Tag);
        }
    }
}
